/**
 * @fileoverview
 * TODO doc me
 */


// Shortcuts
var log = YAHOO.log;
var assert = YAHOO.util.Assert;

function trace(message) { YAHOO.log(message, "test"); }

// custom assertions

assert.isVisible = function(element, message) {
	assert.isFalse(YAHOO.util.Dom.hasClass(element, "invisible"), message);
};

assert.isNotVisible = function(element, message) {
	assert.isTrue(YAHOO.util.Dom.hasClass(element, "invisible"), message);
};


// ************************************************************
// MODULE TESTS
// ************************************************************


/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.ModuleTests = function(){};


/** 
 * Test that the contents of the flyui.flyted.ImageWidget module have been defined.
 */
flyui.flyted.ImageWidget.ModuleTests.testModuleContents = function() {
	
	try {
		flyui.info("Test \"==== flyui.flyted.ImageWidget ModuleTests :: testModuleContents ====\" started.");
		
		assert.isNotUndefined(flyui.flyted.ImageWidget, "flyui.flyted.ImageWidget is undefined");	
		assert.isNotUndefined(flyui.flyted.ImageWidget.Controller, "flyui.flyted.ImageWidget.Controller is undefined");	
		assert.isNotUndefined(flyui.flyted.ImageWidget.modelDefinition, "flyui.flyted.ImageWidget.modelDefinition is undefined");	
		assert.isNotUndefined(flyui.flyted.ImageWidget.DefaultRenderer, "flyui.flyted.ImageWidget.DefaultRenderer is undefined");	
	} catch (error) {
        	throw new flyui.UnexpectedException("flyui.flyted.ImageWidget.ModuleTests.testModuleContents", error);
    }
};


/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.ModuleTestCase = function (){
	try {
		var testCase = new YAHOO.tool.TestCase({
		
		name: "=== flyui.flyted.ImageWidget ModuleTests ===",
		
		testModuleContents : flyui.flyted.ImageWidget.ModuleTests.testModuleContents
		
		});
	
		return testCase;
	} catch (error) {
        	throw new flyui.UnexpectedException("flyui.flyted.ImageWidget.ModuleTestCase", error);
    }
};


// ************************************************************
// CONTROLLER TESTS
// ************************************************************


/**
 * @class
 * A collection of tests to test the Controller class.
 */
flyui.flyted.ImageWidget.ControllerTests = function() {};
 //

/** 
 * Set up function for the flyui.flyted.ImageWidget.ControllerTests.
 * @param {String} endpointURL URL of flyted service
 * @param {YAHOO.tool.TestCase} testCase the test case context
 */
flyui.flyted.ImageWidget.ControllerTests.setUpTest = function( endpointURL, testCase ) {
	try {
		trace("setUp test");
		// create a model
		testCase.model = new flyui.mvcutils.GenericModel2();
		testCase.model.setDefinition(flyui.flyted.ImageWidget.modelDefinition);
	
		// create a service
		testCase.service = new flyui.flyted.Service(endpointURL);
		
		// create a dummy widget
		testCase.dummyWidget = {};
		
		// create a new controller instance
		testCase.controller = new flyui.flyted.ImageWidget.Controller(testCase.model, testCase.service, testCase.dummyWidget);
		
	} catch (e) {
		flyui.err("setUp error, "+e.name+", "+e.message);
	}
};


/** 
 * Tear down function for the flyui.flyted.ImageWidget.ControllerTests.
 * @param {YAHOO.tool.TestCase} testCase the test case context
 */
flyui.flyted.ImageWidget.ControllerTests.tearDownTest = function( testCase ) {
	try {
		trace("tearDown test");
	} catch (e) {
		flyui.err("tearDown error, "+e.name+", "+e.message);
	}
};


/**
 * Test the model definition and initialisation.
 * @synchronous
 */
flyui.flyted.ImageWidget.ControllerTests.testModelInit = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget ControllerTests :: testModelInit ====\" started.");

	// create a model
	var model = testCase.model;

	// check the model properties
	assert.areEqual("READY", model.get("STATE"));
	assert.isNull(model.get("RESULTS"));
	assert.isNull(model.get("ERRORMESSAGE"));
	assert.isNull(model.get("QUERY"));

};


/**
 * Test the Controller class constructor.
 * @param {YAHOO.tool.TestCase} testCase the test case context
 * @synchronous
 */
flyui.flyted.ImageWidget.ControllerTests.testControllerConstructor = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget ControllerTests :: testControllerConstructor ====\" started.");

	// create a new controller instance
	var controller = testCase.controller;

	// check the API
	assert.isNotUndefined(controller.findImagesByFlybaseGeneID);
	assert.isNotUndefined(controller.findImagesByFlytedGeneName);
	
};

/**
 * TODO doc me
 * @asynchronous
 */
flyui.flyted.ImageWidget.ControllerTests.test_findImagesByFlytedGeneName = function( testCase ) {

	flyui.info("Test \"==== flyui.flyted.ImageWidget ControllerTests :: test_findImagesByFlytedGeneName ====\" started.");

	// convenience variables
	var controller = testCase.controller;
	var model = testCase.model;
	
	var success = function(images) {
		trace("success case, resume test runner");
		testCase.resume();
	};
	
	var failure = function(response) {
		trace("failure case, resume test runner");
		testCase.resume(function() {
			assert.fail("request failed, status "+response.status+" "+response.statusText);
		});
	};
	
	// call the private method implementing the find function
	controller._findImagesByFlytedGeneName("foo", success, failure);
	
	// check the model state property
	assert.areEqual("PENDING", model.get("STATE"));
	
	// check the model query property
	assert.areEqual("foo", model.get("QUERY"));
	
	trace("suspend test runner (hang here means problem)");
	testCase.wait();	
	
};


flyui.flyted.ImageWidget.ControllerTests.test_findImagesByFlybaseGeneID = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget ControllerTests :: test_findImagesByFlyBaseGeneID ====\" started.");

	// convenience variables
	var controller = testCase.controller;
	var model = testCase.model;
	
	var success = function(images){
		trace("success case, resume test runner");
		testCase.resume();
	}
	
	var failure = function (response) {
		trace("failure case, resume test runner");
		testCase.resume(function() {
			assert.fail("request failed, status "+response.status+" "+response.statusText);
		});
	}
	
	controller._findImagesByFlybaseGeneID("foo", success, failure);
	
	// check the model state property
	assert.areEqual("PENDING", model.get("STATE"));
	
	// check the model query property
	assert.areEqual("foo", model.get("QUERY"));
	
	trace("suspend test runner (hang here means problem)");
	testCase.wait();
	
};

/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.ControllerTests.test_findImagesSuccess = function (testCase) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget ControllerTests :: test_findImagesSuccess ====\" started.");

	// convenience variables
	var controller = testCase.controller;
	var model = testCase.model;
	
	// fake up the state as pending
	model.set("STATE", "PENDING");
	
	// fake up a result set
	var images = [];
	
	// make the callback
	controller._findImagesSuccess(images);
	
	// check the state
	assert.areEqual("READY", model.get("STATE"));
	
	// check the results
	var results = model.get("RESULTS");
	assert.isNotUndefined(results);
	assert.isNotUndefined(results.length);
	assert.areEqual(0, results.length);
	
};

/**
 * TODO doc me
 */

flyui.flyted.ImageWidget.ControllerTests.test_findImagesFailure = function (testCase) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget ControllerTests :: test_findImagesFailure ====\" started.");

	var controller = testCase.controller;
	var model = testCase.model;

	// fake up the state as pending
	model.set("STATE", "PENDING");
		
	// make the call
	var response = {status:999, statusText:"dummy error for testing"};
	controller._findImagesFailure(response);

	// check the state
	assert.areEqual("SERVERERROR", model.get("STATE"));
	
	// check the error message
	var msg = model.get("ERRORMESSAGE");
	trace("error message: "+msg);
	assert.isNotNull(msg);
};

///**
// * TODO doc me
// */
//flyui.flyted.ImageWidget.ControllerTests.test_findImagesByFlytedGeneNameSuccess = function( testCase ) {
//	flyui.info("Test \"==== flyui.flyted.ImageWidget ControllerTests :: test_findImagesByFlytedGeneNameSuccess ====\" started.");
//
//	// convenience variables
//	var controller = testCase.controller;
//	var model = testCase.model;
//	
//	// fake up the state as pending
//	model.set("STATE", "PENDING");
//	
//	// fake up a result set
//	var images = [];
//	
//	// make the callback
//	controller._findImagesByFlytedGeneNameSuccess(images);
//	
//	// check the state
//	assert.areEqual("READY", model.get("STATE"));
//	
//	// check the results
//	var results = model.get("RESULTS");
//	assert.isNotUndefined(results);
//	assert.isNotUndefined(results.length);
//	assert.areEqual(0, results.length);
//
//};


///**
// * TODO doc me
// */
//flyui.flyted.ImageWidget.ControllerTests.test_findImagesByFlytedGeneNameFailure = function( testCase ) {
//	flyui.info("Test \"==== flyui.flyted.ImageWidget ControllerTests :: test_findImagesByFlytedGeneNameFailure ====\" started.");
//
//	var controller = testCase.controller;
//	var model = testCase.model;
//
//	// fake up the state as pending
//	model.set("STATE", "PENDING");
//		
//	// make the call
//	var response = {status:999, statusText:"dummy error for testing"};
//	controller._findImagesByFlytedGeneNameFailure(response);
//
//	// check the state
//	assert.areEqual("SERVERERROR", model.get("STATE"));
//	
//	// check the error message
//	var msg = model.get("ERRORMESSAGE");
//	trace("error message: "+msg);
//	assert.isNotNull(msg);
//
//};


/** 
 * Construct a YUI test case for the flyui.flyted.ImageWidget ControllerTests.
 * @param {String} endpointURL URL of service
 * @return a YUI test case
 * @type YAHOO.tool.TestCase
 */
flyui.flyted.ImageWidget.ControllerTestCase = function( endpointURL ) {
	
	var testCase = new YAHOO.tool.TestCase({
		
		name: "=== flyui.flyted.ImageWidget ControllerTests ===",
		
		setUp : function() {
			flyui.flyted.ImageWidget.ControllerTests.setUpTest(endpointURL, this);
		},
		
		tearDown : function() {
			flyui.flyted.ImageWidget.ControllerTests.tearDownTest(this);
		},
		
		testModelInit : function() {
			flyui.flyted.ImageWidget.ControllerTests.testModelInit(this);
		},
		
		testControllerConstructor : function() {
			flyui.flyted.ImageWidget.ControllerTests.testControllerConstructor(this);
		},
				
		test_findImagesByFlytedGeneName : function() {
			flyui.flyted.ImageWidget.ControllerTests.test_findImagesByFlytedGeneName(this);
		},
		
		test_findImagesSuccess : function() {
			flyui.flyted.ImageWidget.ControllerTests.test_findImagesSuccess(this);
		},
		
		test_findImagesFailure : function() {
			flyui.flyted.ImageWidget.ControllerTests.test_findImagesFailure(this);
		},
		
		test_findImagesByFlyBaseGeneID : function(){
			flyui.flyted.ImageWidget.ControllerTests.test_findImagesByFlybaseGeneID (this);
		}
//		test_findImagesByFlybaseGeneIDSuccess : function(){
//			flyui.flyted.ImageWidget.ControllerTests.test_findImagesByFlybaseGeneIDSuccess(this);
//		},
//		test_findImagesByFlybaseGeneIDFailure : function(){
//			flyui.flyted.ImageWidget.ControllerTests.test_findImagesByFlybaseGeneIDFailure(this);
//		}
		
	});
	
	return testCase;
	
};


// ************************************************************
// DEFAULT RENDERER TESTS
// ************************************************************


/**
 * @class
 * A collection of tests to test the DefaultRenderer class.
 */
flyui.flyted.ImageWidget.DefaultRendererTests = function() {};


/** 
 * Set up function for the flyui.flyted.ImageWidget DefaultRendererTests.
 * @param {YAHOO.tool.TestCase} testCase the test case context
 */
flyui.flyted.ImageWidget.DefaultRendererTests.setUpTest = function( testCase ) {
	try {
		trace("setUp test");
		
		trace("create a model");
		testCase.model = new flyui.mvcutils.GenericModel2();
		testCase.model.setDefinition(flyui.flyted.ImageWidget.modelDefinition);
		
		trace("create an element for the renderer");
		testCase.renderPane = document.createElement("div");
		
		trace("put the renderer's div into the dom");
		document.body.appendChild(testCase.renderPane);
		
		trace("create a renderer");
		testCase.renderer = new flyui.flyted.ImageWidget.DefaultRenderer();
		testCase.renderer.setCanvas(testCase.renderPane);
		
		trace("connect the renderer the model");
		testCase.renderer.connect(testCase.model);
				
	} catch (e) {
		flyui.err("setUp error, "+e.name+", "+e.message);
	}
};


/** 
 * Tear down function for the flyui.flyted.ImageWidget DefaultRendererTests.
 * @param {YAHOO.tool.TestCase} testCase the test case context
 */
flyui.flyted.ImageWidget.DefaultRendererTests.tearDownTest = function( testCase ) {
	try {
		trace("tearDown test");
		
		trace("remove the renderer element from the dom");
		document.body.removeChild(testCase.renderPane); 
		
	} catch (e) {
		flyui.err("tearDown error, "+e.name+", "+e.message);
	}
};


/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.DefaultRendererTests.testAPI = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testAPI ====\" started.");
		
	var renderer = testCase.renderer;
	
	assert.isNotUndefined(renderer);
	assert.isNotUndefined(renderer.connect);

	assert.isNotUndefined(renderer._onStateChanged);
	assert.isNotUndefined(renderer._onQueryChanged);
	assert.isNotUndefined(renderer._onResultsChanged);
	assert.isNotUndefined(renderer._onErrorMessageChanged);

	// check pane fields
	assert.isNotUndefined(renderer._canvas);
	assert.isNotUndefined(renderer._pendingPane);
	assert.isNotUndefined(renderer._messagePane);
	assert.isNotUndefined(renderer._resultsSummaryPane);
	assert.isNotUndefined(renderer._resultsPane);

};

/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.DefaultRendererTests.testMainComponents = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testMainComponents ====\" started.");

	var renderPane = testCase.renderPane;
	var renderer = testCase.renderer;
	
	var children = renderPane.childNodes;
	trace(renderPane.innerHTML);
	for ( var i=0; i<children.length; i++ ) {
		trace("found child: "+children[i]);
	}
	assert.areEqual(5, children.length, "number of child elements not as expected");
	
	trace("check first DIV is hidden'");
	var pendingPane = children[0];
	assert.isNotVisible(pendingPane, "pending pane should be invisible");
//	assert.areEqual(1, pendingPane.childNodes.length);
//	assert.areEqual("Pending...", pendingPane.childNodes[0].innerHTML);
	
	trace("Check 2nd DIV is invisible and contains text 'this should never be displayed'");
	assert.isNotVisible(children[1], "Message pane should be invisible");
	assert.areEqual("this should never be displayed", children[1].innerHTML);

	trace("Check 3rd DIV is invisible");
	assert.isNotVisible(children[2], "Results summary pane should be invisible");

	trace("Check 4th DIV is invisible");
	assert.isNotVisible(children[3], "Results pane should be invisible");

//	trace("Check 2nd DIV using Yahoo selector for access");
//	var divs = YAHOO.util.Selector.query("div", renderPane);
//	assert.isNotVisible(divs[1], "Message div should be invisible");
//	assert.areEqual("this should never be displayed", divs[1].innerHTML);
	
};

/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.DefaultRendererTests.testModelStateChangeToPending = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testModelStateChangeToPending ====\" started.");
	
	var renderPane = testCase.renderPane;
	var model = testCase.model;
	var children = renderPane.childNodes;
	
	trace("set the model state to pending");
	model.set("STATE", "PENDING");
	
	trace("check the pending pane is visible");
	assert.isVisible(children[0], "Pending pane should be visible");

	trace("check the message pane is not visible");
	assert.isNotVisible(children[1], "message pane should be invisible");

	trace("check the results summary pane is not visible");
	assert.isNotVisible(children[2], "results summary pane should be invisible");

	trace("check the results pane is not visible");
	assert.isNotVisible(children[3], "results pane should be invisible");
};


/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.DefaultRendererTests.testModelStateChangeToReady = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testModelStateChangeToReady ====\" started.");
	
	var renderPane = testCase.renderPane;
	var model = testCase.model;
	var children = renderPane.childNodes;
	
	trace("set the model state to ready");
	model.set("STATE", "READY");
	
	trace("check the pending pane is not visible");
	assert.isNotVisible(children[0], "Pending pane should be invisible");

	trace("check the message pane is not visible");
	assert.isNotVisible(children[1], "message pane should be invisible");

	trace("check the results summary pane is visible");
	assert.isVisible(children[2], "results summary pane should be visible");

    // N.B. results pane only visible if >0 results
//	trace("check the results pane is visible");
//	assert.isVisible(children[3], "results pane should be visible");	
};

/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.DefaultRendererTests.testModelStateChangeToServerError = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testModelStateChangeToServerError ====\" started.");
	
	var renderPane = testCase.renderPane;
	var model = testCase.model;
	var children = renderPane.childNodes;
	
	trace("set the model state to pending");
	model.set("STATE", "PENDING");	
	
	trace("set the model state to server error");
	model.set("STATE", "SERVERERROR");
	
	trace("check the pending pane is not visible");
	assert.isNotVisible(children[0], "Pending pane should be invisible");

	trace("check the message pane is visible");
	assert.isVisible(children[1], "message pane should be visible");

	trace("check the results summary pane is not visible");
	assert.isNotVisible(children[2], "results summary pane should be invisible");

	trace("check the results pane is not visible");
	assert.isNotVisible(children[3], "results pane should be invisible");
};

/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.DefaultRendererTests.testModelStateChangeToUnexpectedError = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testModelStateChangeToUnexpectedError ====\" started.");
	
	var renderPane = testCase.renderPane;
	var model = testCase.model;
	var children = renderPane.childNodes;
	
	trace("set the model state to pending");
	model.set("STATE", "PENDING");	
	
	trace("set the model state to unexpected error");
	model.set("STATE", "UNEXPECTEDERROR");
	
	trace("check the pending pane is not visible");
	assert.isNotVisible(children[0], "Pending pane should be invisible");

	trace("check the message pane is visible");
	assert.isVisible(children[1], "message pane should be visible");

	trace("check the results summary pane is not visible");
	assert.isNotVisible(children[2], "results summary pane should be invisible");

	trace("check the results pane is not visible");
	assert.isNotVisible(children[3], "results pane should be invisible");
};

/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.DefaultRendererTests.testZeroResults = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testZeroResults ====\" started.");
	
	var renderPane = testCase.renderPane;
	var model = testCase.model;
	var children = renderPane.childNodes;
	var summaryPane = children[2];
	var resultsPane = children[3];
	var testData = new flyui.flyted.Service.TestData;
	var images = [];
	
	trace("set the model query");
	model.set("QUERY", "foo");
	
	trace("set the model results");
	model.set("RESULTS", images);
	
	trace("set the state ready");
	model.set("STATE", "READY");
	
	trace("checking the results summary pane");
	flyui.flyted.ImageWidget.assertResultsSummaryPaneAsExpected(0, "foo", summaryPane, null);

	trace("checking the results pane");
	flyui.flyted.ImageWidget.assertResultsPaneAsExpected(images, resultsPane);
	
};

//flyui.flyted.ImageWidget.DefaultRendererTests.testSummaryPane = function( testCase ) {
//	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testSummaryPane ====\" started.");
//		
//	var renderPane = testCase.renderPane;
//	var model = testCase.model;
//	var children = renderPane.childNodes;
//	var summaryPane = children[2];
//	
//	var query = new flyui.flybase.Gene();
//	query.flybaseID = "FBgn0042710";
//	query.symbols = ["Hex-t2"];
//	
//	trace("set the model query");
//	model.set("QUERY", query.flybaseID);
//		
//    trace("set the state ready");
//    model.set("STATE", "READY");
//    
//	trace("checking the results summary pane");
//	flyui.flyted.ImageWidget.assertResultsSummaryPaneAsExpected(4, query, summaryPane, query.symbols);
//}

/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.DefaultRendererTests.testOneResult = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testOneResult ====\" started.");
	
	var renderPane = testCase.renderPane;
	var model = testCase.model;
	var children = renderPane.childNodes;
	var summaryPane = children[2];
	var resultsPane = children[3];
	var testData = new flyui.flyted.Service.TestData;
	var images = [testData.Cdc37_in_wt];
	
	var query = new flyui.flybase.Gene();
	query.flybaseID = "FBgn0011573";
	query.symbols = ["Cdc37"];
	
	trace("set the model query");
	model.set("QUERY", query);
	
	trace("set the model results");
	model.set("RESULTS", images);
	
    trace("set the state ready");
    model.set("STATE", "READY");
    
	trace("checking the results summary pane");
	flyui.flyted.ImageWidget.assertResultsSummaryPaneAsExpected(1, query, summaryPane, ['Cdc37']);

	trace("checking the results pane");
	flyui.flyted.ImageWidget.assertResultsPaneAsExpected(images, resultsPane);
		
};

flyui.flyted.ImageWidget.DefaultRendererTests.testResultFor_Prosalpha6 = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testResultFor_Prosalpha6 ====\" started.");
	
	var renderPane = testCase.renderPane;
	var model = testCase.model;
	var children = renderPane.childNodes;
	var summaryPane = children[2];
	var resultsPane = children[3];
	var testData = new flyui.flyted.Service.TestData;
	var images = [testData.FBgn0050382_in_comr, testData.FBgn0050382_in_wt_1, testData.FBgn0050382_in_wt_2];
	
	var query = new flyui.flybase.Gene();
	query.flybaseID = "FBgn0050382";
	query.symbols = ["CG30382"];
	
	trace("set the model query");
	model.set("QUERY", query);
	
	trace("set the model results");
	model.set("RESULTS", images);
	
    trace("set the state ready");
    model.set("STATE", "READY");
    
	trace("checking the results summary pane");
	flyui.flyted.ImageWidget.assertResultsSummaryPaneAsExpected(3, query, summaryPane, ['prosalpha1', 'Prosalpha6']);

	trace("checking the results pane");
	flyui.flyted.ImageWidget.assertResultsPaneAsExpected(images, resultsPane);
		
};


flyui.flyted.ImageWidget.DefaultRendererTests.testTwoResults = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget DefaultRenderer Tests :: testTwoResults ====\" started.");
	
	var renderPane = testCase.renderPane;
	var model = testCase.model;
	var children = renderPane.childNodes;
	var summaryPane = children[2];
	var resultsPane = children[3];
	var testData = new flyui.flyted.Service.TestData;
	var images = [testData.acer_in_aly, testData.acer_in_wt];
	
	trace("set the model query");
	model.set("QUERY", "acer");
	
	trace("set the model results");
	model.set("RESULTS", images);
	
    trace("set the state ready");
    model.set("STATE", "READY");
    
	trace("checking the results summary pane");
	flyui.flyted.ImageWidget.assertResultsSummaryPaneAsExpected(2, "acer", summaryPane,  null);

	trace("checking the results pane");
	flyui.flyted.ImageWidget.assertResultsPaneAsExpected(images, resultsPane);
		
};


/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.DefaultRendererTests.testSetErrorMessage = function( testCase ) {
	flyui.info("Test \"==== flyui.flyted.ImageWidget.DefaultRenderer Tests :: testSetErrorMessage ====\" started.");
	
	var renderPane = testCase.renderPane;
	var model = testCase.model;
	var children = renderPane.childNodes;
	var messagePane = children[1];

	var msg = "this is a test error message";
	model.set("ERRORMESSAGE", msg);
	
	assert.areEqual(msg, messagePane.innerHTML);
};


/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.assertResultsSummaryPaneAsExpected = function( count, query, summaryPane, labels ) {
	trace("checking the results summary pane");
	trace(summaryPane.innerHTML);
    assert.isVisible(summaryPane);
    
    // do light checking...
	/* check the results summary pane
	 * expect something like...
	 * <p>showing <span>2</span> matching images from <a href='http://www.fly-ted.org'>www.fly-ted.org</a> for query "<span>'rbf'</span>"</div> 
	 * <p>showing <span>2</span> matching images from <a href="http://www.fly-ted.org">www.fly-ted.org</a> for gene <strong>Acer</strong> 
	 * (mapped to FlyTED gene name <strong> <a href="http://www.fly-ted.org/view/geneid/acer.html">acer</a></strong>
	 * , N.B. gene name mappings may contain errors, check source data carefully)</p>
	 */
	 
	trace("the summary pane content:" + summaryPane.innerHTML);
	var spans = YAHOO.util.Selector.query("span", summaryPane);
	assert.areEqual(count, spans[0].innerHTML);
	
	var content = summaryPane.innerHTML;
	
	if (query instanceof flyui.flybase.Gene){
	    
	    for (var i=0; i<labels.length; i++) {
	        var href = "http://www.fly-ted.org/view/geneid/"+labels[i]+".html";
	        trace("looking for link: "+href);
	        var index = content.indexOf(href);
	        assert.isTrue(index>0);
	    }
	    
//		var strongs = YAHOO.util.Selector.query("strong", summaryPane);
//		// test the first strong, the query string
//		var symbols = query.symbols;
//		if (symbols.length == 1){
//			assert.areEqual(query.symbols, strongs[0].innerHTML);	
//		}else{
//			assert.areEqual(query.symbols.join(" / "), strongs[0].innerHTML);
//		}
		
		// test the second strong, the flyted gene names
		
//		if (labels){
//			trace("=== checking the labels ===");
//			var actualLabels = strongs[1];
//			if (labels.length == 1) {
//				var linkToFlyted = actualLabels.childNodes[0];
//				flyui.info("the content of the hyperlink: " + linkToFlyted.href);
//				assert.areEqual("http://www.fly-ted.org/view/geneid/"+labels[0]+".html", linkToFlyted);
//				assert.areEqual(labels[0], linkToFlyted.innerHTML);				
//			}
//        }   
					
	} else {
		assert.areEqual("'"+query+"'", spans[1].innerHTML);
	}
	
	
};


/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.assertResultsPaneAsExpected = function( images, resultsPane ) {
	
	if (images.length > 0) {
	
		/* check the results pane
		 * expect something like...
		 * <div>
		 *   <div class="result">
		 *     <a href="">
		 *       <img src="" alt="this is an image"/>
		 * 	   </a>
		 *     </div>
		 *     ...
		 * </div>
		 */
		var resultNodes = YAHOO.util.Selector.query("div.result", resultsPane);
		trace("check number of rendered results");
		assert.areEqual(images.length, resultNodes.length); // expect same number of div boxes as results
		
		for ( var i=0; i<images.length; i++ ) {
			trace("checking rendered result "+(i+1)+": "+resultNodes[i].innerHTML);
			
			flyui.flyted.ImageWidget.assertImageDivAsExpected(images[i], resultNodes[i]); 
		}

	}
	else {
		assert.areEqual("", resultsPane.innerHTML); // if no images, pane is empty
	}
		
};


/**
 * TODO doc me
 */
flyui.flyted.ImageWidget.assertImageDivAsExpected = function (image, resultDivNode) {
	trace("check the rendered result values");
		
	/*
	 * expect something like...
	 * <p><a href=""><img src="" alt="this is an image"></a></p><p>[caption]</p>
	 */
	
	var linkNode = resultDivNode.childNodes[0].childNodes[0];
	assert.areEqual(image.flytedURL, linkNode.href);
	
	trace("image hyperlink: "+ linkNode.innerHTML);
	
	var imgNode = linkNode.childNodes[0];
	trace("thumbnail " + imgNode.src);
	assert.areEqual(image.thumbnailURL, imgNode.src);
		
};


/** 
 * Construct a YUI test case for the flyui.flyted.ImageWidget DefaultRendererTests.
 * @return a YUI test case
 * @type YAHOO.tool.TestCase
 */
flyui.flyted.ImageWidget.DefaultRendererTestCase = function() {
	
	var testCase = new YAHOO.tool.TestCase({
		
		name: "=== flyui.flyted.ImageWidget DefaultRenderer Tests ===",
		
		setUp : function() {
			flyui.flyted.ImageWidget.DefaultRendererTests.setUpTest(this);
		},
		
		tearDown : function() {
			flyui.flyted.ImageWidget.DefaultRendererTests.tearDownTest(this);
		},
		
		testAPI : function() {
			flyui.flyted.ImageWidget.DefaultRendererTests.testAPI(this);
		},
		
		testMainComponents : function(){
			flyui.flyted.ImageWidget.DefaultRendererTests.testMainComponents(this);
		},
		
		testModelStateChangeToPending : function(){
			flyui.flyted.ImageWidget.DefaultRendererTests.testModelStateChangeToPending(this);
		},
		
		testModelStateChangeToReady : function(){
			flyui.flyted.ImageWidget.DefaultRendererTests.testModelStateChangeToReady(this);
		},
		
		testModelStateChangeToServerError : function(){
			flyui.flyted.ImageWidget.DefaultRendererTests.testModelStateChangeToServerError(this);
		},
		
		testModelStateChangeToUnexpectedError : function() {
			flyui.flyted.ImageWidget.DefaultRendererTests.testModelStateChangeToUnexpectedError(this);
		},
		
		testResultFor_Prosalpha6: function() {
			flyui.flyted.ImageWidget.DefaultRendererTests.testResultFor_Prosalpha6(this);
		},
		
		testZeroResults : function() {
			flyui.flyted.ImageWidget.DefaultRendererTests.testZeroResults(this);
		},
		
		testOneResult : function() {
			flyui.flyted.ImageWidget.DefaultRendererTests.testOneResult(this);
		},
		
		testTwoResults : function(){
			flyui.flyted.ImageWidget.DefaultRendererTests.testTwoResults(this);
		}, 
		
		testSetErrorMessage : function() {
			flyui.flyted.ImageWidget.DefaultRendererTests.testSetErrorMessage(this);
		}
				
	});
	
	return testCase;
	
};

// ************************************************************
// TEST SUITE AND RUNNER
// ************************************************************


/** 
 * Construct a YUI test suite for the flyui.flyted.ImageWidget module.
 * @param {String} endpointURL URL of flyted service
 * @return a YUI test suite
 * @type YAHOO.tool.TestSuite
 */
flyui.flyted.ImageWidget.TestSuite = function(endpointURL) {
	try {
		var suite = new YAHOO.tool.TestSuite("== flyui.flyted.ImageWidget Test Suite ==");
		suite.add(flyui.flyted.ImageWidget.ModuleTestCase());
		suite.add(flyui.flyted.ImageWidget.ControllerTestCase(endpointURL));
		suite.add(flyui.flyted.ImageWidget.DefaultRendererTestCase());
		return suite;
	} catch (error) {
        	throw new flyui.UnexpectedException("flyui.flyted.ImageWidget.TestSuite", error);
    }
};


/** 
 * Run the flyui.flyted.ImageWidget test suite.
 * @param {String} endpointURL URL of flyted service
 */
flyui.flyted.ImageWidget.runTests = function(endpointURL) {
	try {
		trace("flyui.flyted.ImageWidget :: running tests");
		YAHOO.tool.TestRunner.clear();
		YAHOO.tool.TestRunner.add(flyui.flyted.ImageWidget.TestSuite(endpointURL));
		YAHOO.tool.TestRunner.run();
	} catch (error) {
        	throw new flyui.UnexpectedException("flyui.flyted.ImageWidget.runTests", error);
    }
};


